package com.example.petadoption.Controller;

import com.example.petadoption.Entity.Pet;
import com.example.petadoption.Service.PetService;
import jakarta.validation.Valid;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
//import jakarata.validation.Valid;
import java.net.URI;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/api/pets")
public class PetController {
    private static final Logger logger = LoggerFactory.getLogger(PetController.class);

    @Autowired
    private PetService petService;

    @GetMapping
    public List<Pet> getAvailablePets() {
        logger.info("正在查询可领养宠物列表");
        return petService.getAvailablePets();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<Pet> addPet(@Valid @RequestBody Pet pet) {
        try {
            Pet savedPet = petService.addPet(pet);
            return ResponseEntity.created(URI.create("/pets/" + savedPet.getId()))
                    .body(savedPet);
        } catch (Exception e) {
            logger.error("添加宠物失败", e);
            throw e; // 由全局处理器处理
        }
    }
}